<?php
namespace Common\Model;
use Think\Model;

/**
 * 店铺->首页-》专题
 * Class SpecialStoreModel
 * @package Common\Model
 */
class SpecialStoreModel extends Model  {

    protected $tableName = 'b2c_special_store';

    /**
     * 通local_id获取专题信息
     * @param $local_id
     */
    public function getMsgFromLocalId($local_id){

        $time = time();
        $where = [
            'a.local_id'=>$local_id,
            'a.state'=>1,
            'a.start_time'=> ['ELT', time()],
            'a.end_time'=> ['GT', time()],
        ];
        $list = M('b2c_special_store a')
            ->cache(true, 86400, 'redis')
            ->join(C('DB_PREFIX').'b2c_special_template b on a.tid=b.id')
            ->field('a.id, a.title, a.title2, a.img_url,a.price_desc,a.end_time, b.title t_title, b.title2 t_title2, b.img_url t_img_url', 'left')
            ->order('a.idx asc')
            ->where($where)
            ->select();
        if (!empty($list)) {
            foreach($list as &$item) {
                $item['title'] = $item['t_title'] ? $item['t_title']: $item['title'];
                $item['title2'] = $item['t_title2'] ? $item['t_title2']: $item['title2'];
                $item['img_url'] = $item['t_img_url'] ? $item['t_img_url']: $item['img_url'];
                unset($item['t_title'],$item['t_title2'], $item['t_img_url']);
            }
        }
        foreach ($list as &$val) {
            $tmp = (int)ceil(($val['end_time'] - $time) / 86400);
            $val['end_time'] = $tmp-1 > 0 ? $tmp : 1;
        }
        return $list;
    }


}